package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crm.common.constant.UserConstants;
import com.crm.entity.Business;
import com.crm.entity.Clue;
import com.crm.entity.Contract;
import com.crm.mapper.ClueMapper;
import com.crm.mapper.ContractMapper;
import com.crm.service.BusinessService;
import com.crm.service.ClueService;
import com.crm.service.ContractService;
import com.crm.service.IndexService;
import com.crm.vo.BasisStatisticsVO;
import com.crm.vo.SalesStatisticVO;
import com.crm.vo.VulnerabilityVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * \* User: chy
 * \* Date: 2025/9/5  16:19
 * \* Description:
 * \
 */
@Service
@RequiredArgsConstructor
public class IndexServiceImpl  implements IndexService {

    private final ClueService clueService;
    private final BusinessService businessService;
    private final ContractMapper contractMapper;
    private final ContractService contractService;
    private final ClueMapper clueMapper;



    @Override
    public BasisStatisticsVO getBaseInfo(LocalDate beginTime, LocalDate endTime) {

        LocalDateTime begin = getStartOfDay(beginTime);
        LocalDateTime end = getEndOfDay(endTime);

        // 获取今天23:59:59
       LocalDateTime todayBeginTime = LocalDate.now().atTime(0, 0, 0);
        LocalDateTime todayEndTime  = LocalDate.now().atTime(23, 59, 59);

        // 查询线索数
        long clueCount = clueService.count(new LambdaQueryWrapper<Clue>()
                .and(QueryWrapper -> QueryWrapper.isNull(Clue::getTransfer).or().ne(Clue::getTransfer, UserConstants.CLUE_TRANSFER_YES))
                .between( Clue::getCreateTime, begin, end));

        // 查询商机数
        long businessCount = businessService.count( new LambdaQueryWrapper<Business>().between(Business::getCreateTime, begin, end));

        // 统计合同总数
        long contractNum = contractService.count(new LambdaQueryWrapper<Contract>().between(Contract::getCreateTime, begin, end));

        // 统计时间范围销售总额
        Double salesAmount = contractMapper.sumAmount(begin ,end);
        // 获取今天线索数
        Long todayCluesNum = getTodayCluesNum(todayBeginTime, todayEndTime);
        // 获取今天商机数
        Long todayBusinessNum = getTodayBusinessNum(todayBeginTime, todayEndTime);
        // 获取今天合同数
        Long todayContractNum = getTodayContractNum(todayBeginTime, todayEndTime);
        // 获取今天销售总额
        Double todaySalesAmount = contractMapper.sumAmount(todayBeginTime ,todayEndTime);
        // 获取待跟进线索    .between(Clue::getCreateTime, begin, end)      todo:具体是要所有数据还是当前时间段的数据
        Long tofollowedCluesNum = clueService.count(new LambdaQueryWrapper<Clue>().eq(Clue::getStatus, UserConstants.CLUE_STATUS_WAIT));
        // 获取待分配线索       .between(Clue::getCreateTime, begin, end)   todo:具体是要所有数据还是当前时间段的数据
        Long toallocatedCluesNum = clueService.count(new LambdaQueryWrapper<Clue>().eq(Clue::getOwner,null));
        // 获取待跟进商机  TODO:未完善
        //获取待分配商机  TODO:未完善

        return BasisStatisticsVO.builder()
                .cluesNum(clueCount)
                .businessNum(businessCount)
                .contractNum(contractNum)
                .salesAmount(salesAmount)
                .todayCluesNum(todayCluesNum)
                .todayBusinessNum(todayBusinessNum)
                .todayContractNum(todayContractNum)
                .todaySalesAmount(todaySalesAmount)
                .toallocatedCluesNum(toallocatedCluesNum)
                .tofollowedCluesNum(tofollowedCluesNum)
                .toallocatedBusinessNum(null)
                .build();
    }

    /**
     * 获取漏斗信息
     * @return
     */
    @Override
    public VulnerabilityVO getVulnerabilityMap( LocalDate beginTime,LocalDate endTime) {
        LocalDateTime startDay = getStartOfDay(beginTime);
        LocalDateTime endDay = getEndOfDay(endTime);

        // 查询线索数
        long clueNums = clueService.count(new LambdaQueryWrapper<Clue>()
                .between( Clue::getCreateTime, startDay, endDay));
        // 有效线索数
        long effectiveCluesNums = clueService.count(new LambdaQueryWrapper<Clue>()
                .and(QueryWrapper -> QueryWrapper.isNull(Clue::getTransfer).or().ne(Clue::getTransfer, UserConstants.CLUE_TRANSFER_YES))
                .between( Clue::getCreateTime, startDay, endDay));

        // 查询商机数
        long businessNums = businessService.count( new LambdaQueryWrapper<Business>().between(Business::getCreateTime, startDay, endDay));

        //查询合同数
        long contractNums = contractService.count(new LambdaQueryWrapper<Contract>().between(Contract::getCreateTime, startDay, endDay));


        return VulnerabilityVO.builder()
                .contractNums(contractNums)
                .cluesNums(clueNums)
                .businessNums(businessNums)
                .effectiveCluesNums(effectiveCluesNums)
                .build();
    }

    /**
     * 获取销售统计信息
     * @return
     */
    @Override
    public List<SalesStatisticVO> getSalesStatistic(LocalDate beginCreateTime, LocalDate endCreateTime) {
        LocalDateTime begin = getStartOfDay(beginCreateTime);
        LocalDateTime end = getEndOfDay(endCreateTime);
        List<SalesStatisticVO> basisStatisticsVO =  clueMapper.getSalesStatistic(begin,end);
        return basisStatisticsVO;
    }

    /**
     * 获取指定日期的开始时间 (00:00:00)
     * @param date 日期
     * @return LocalDateTime 开始时间
     */
    private LocalDateTime getStartOfDay(LocalDate date) {
        return date.atStartOfDay();
    }

    /**
     * 获取指定日期的结束时间 (23:59:59)
     * @param date 日期
     * @return LocalDateTime 结束时间
     */
    private LocalDateTime getEndOfDay(LocalDate date) {
        return date.atTime(23, 59, 59);
    }

    /**
     * 获取今天合同数
     * @param todayBeginTime  todayEndTime
     */
    private Long getTodayContractNum(LocalDateTime todayBeginTime, LocalDateTime todayEndTime) {

        return contractService.count(new LambdaQueryWrapper<Contract>()
                .between(Contract::getCreateTime, todayBeginTime, todayEndTime));
    }

    /**
     * 获取今天商机数
     * @param todayBeginTime  todayEndTime
     */
    private Long getTodayBusinessNum(LocalDateTime todayBeginTime, LocalDateTime todayEndTime) {
        return businessService.count(new LambdaQueryWrapper<Business>()
                .between(Business::getCreateTime, todayBeginTime, todayEndTime));
    }

    /**
     * 获取今天线索数
     * @param todayBeginTime  todayEndTime
     */
    private Long getTodayCluesNum(LocalDateTime todayBeginTime, LocalDateTime todayEndTime) {
        return clueService.count(new LambdaQueryWrapper<Clue>()
                .and(QueryWrapper -> QueryWrapper.isNull(Clue::getTransfer).or().ne(Clue::getTransfer, 1))
                .between(Clue::getCreateTime, todayBeginTime, todayEndTime));
    }
}
